package JFrame;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

import DAO.PurchaseImpl;
import DBConn.DBConn;
import MySQL.Purchase;;

public class purchaseDelete extends JFrame{
	private static final long serialVersionUID = 6393669456611589484L;
	JLabel e1,msg;
	JTextField t1;
	JButton b1,b2;
	Connection conn;
	public purchaseDelete(){
		conn = DBConn.open();
		e1 = new JLabel("商品代码:");
		msg = new JLabel("");
		t1 = new JTextField(10);
		t1.setText("");
		b1 = new JButton("查询");
		b2 = new JButton("删除");
		
		//字体设置
		Font f = new Font("隶书",Font.BOLD,23);
		e1.setFont(f);  t1.setFont(f);
		b1.setFont(f);  b2.setFont(f);
		msg.setFont(f);
		msg.setForeground(Color.lightGray);
		b1.setForeground(Color.blue);  b2.setForeground(Color.blue);
		

		//位置
		e1.setBounds(80, 20,110,30); t1.setBounds(205, 20,200, 30); 
		b1.setBounds(205, 80,90, 40); 
		msg.setBounds(50, 140,500,30); b2.setBounds(205,200,90,40);
		this.add(e1);  this.add(t1);
		this.add(b1);  this.add(msg);
		
		//按钮监听
		b1.addActionListener(new B1());
		b2.addActionListener(new B2());
		
		//窗体设置
		this.setBounds(440,190,550,380);
		setTitle("删除账单");
		this.setLayout(null); // 绝对布局
		setVisible(true);
	}
	class B1 implements ActionListener{
		Purchase p = new Purchase();
		PurchaseImpl pdi = new PurchaseImpl();
		String s = "";
		public void actionPerformed(ActionEvent e)
		{
			try{
				p.setId(t1.getText());
				p = pdi.getPurchaseById(p.getId());
				
				s = "查询成功,是否删除账单："+p.getId()+"?";
				msg.setText(s);
				add(b2);
			}catch(Exception e1) {
				msg.setText("不存在此商品！");
				t1.requestFocus();
			}finally{
				DBConn.close(conn);  //关闭数据库连接
			}
		}
	}
	class B2 implements ActionListener
	{
		Purchase p = new Purchase();
		PurchaseImpl pdi = new PurchaseImpl();
		public void actionPerformed(ActionEvent e)
		{
			try{
				p.setId(t1.getText());
				pdi.delete(p.getId());
				msg.setText("删除成功！");
				t1.setText("");
				t1.requestFocus();
				} catch (Exception e1) {
					msg.setText("输入的Id不存在，请重新输入");
					t1.requestFocus();
				}
		}
	}
}
